/**
 * 
 */
package model.container;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import model.Cursus;
import persistenty.ContainerTekstPersisteerbaar;

/**
 * This class is intended to setup the Cursussen container object.
 * 
 * @author Nicolas Vanderhulst
 * @version 20111009
 * 
 */
public class Cursussen extends ContainerTekstPersisteerbaar implements Comparable<Cursussen>, Iterable<Cursus>,
		Cloneable {
	public HashSet<Cursus> cursussen;
	private int cursusID = 0;

	/**
	 * @return the cursusID
	 */
	public int getCursusID() {
		return cursusID;
	}

	/**
	 * @param cursusID
	 *            the cursusID to set
	 */
	public void setCursusID(int cursusID) {
		this.cursusID = cursusID;
	}

	public Cursussen() throws CursussenException {
		cursussen = new HashSet<Cursus>();
		persistentyFiles = new ArrayList<String>();
		persistentyFiles.add("src/persitentyfiles/Cursus.txt");
	}

	public void add(Cursus c) throws CloneNotSupportedException, CursussenException {
		if (c.getCursusCode() <= this.cursusID) {
			c.setCursusCode(cursusID++);
		}
		if (!cursussen.add(c)) {
			throw new CursussenException("Deze cursuscode bestaat reeds");
		}
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(String.format("src/persitentyfiles/%s.txt", c
					.getClass().getSimpleName()), true));
			writer.append(c.maakTekstVanObject());
			writer.close();
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public void remove(Cursus c) {
		cursussen.remove(c);
	}

	public boolean checkCursusCodeExists(Cursus a) throws CloneNotSupportedException {
		boolean b = false;
		for (Cursus c : cursussen) {
			if (c.getCursusCode() == a.getCursusCode()) {
				b = true;
			}
		}
		return b;
	}

	public String[] getCursussen() {
		int i = 0;
		String[] cursussenAlsString = new String[cursussen.size()];
		Iterator<Cursus> it = cursussen.iterator();
		while (it.hasNext()) {
			Cursus c = it.next();
			cursussenAlsString[i] = c.getCursusNaam();
			i++;
		}
		return cursussenAlsString;
	}

	public Cursus getCursusByCode(int cursusCode) {
		Cursus tempCursus = null;
		for (Cursus c : this.cursussen) {
			if (c.getCursusCode() == cursusCode) {
				tempCursus = c.clone();
			}
		}
		return tempCursus;
	}

	@Override
	public Iterator<Cursus> iterator() {
		return cursussen.iterator();
	}

	@Override
	public int compareTo(Cursussen arg0) {
		return this.toString().compareTo(arg0.toString());
	}

	@Override
	public void schrijven() throws IOException {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter("src/persitentyfiles/Cursus.txt", false));
			for (Cursus c : cursussen) {
				writer.write(c.maakTekstVanObject());
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cursussen == null) ? 0 : cursussen.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Cursussen)) {
			return false;
		}
		Cursussen other = (Cursussen) obj;
		if (cursussen == null) {
			if (other.cursussen != null) {
				return false;
			}
		} else if (!cursussen.equals(other.cursussen)) {
			return false;
		}
		return true;
	}

	/**
	 * @param cursussen
	 *            the cursussen to set
	 */
	public void setCursussen(HashSet<Cursus> cursussen) {
		this.cursussen = cursussen;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Cursussen [cursussen=" + cursussen + "]";
	}

	/**
	 * @return Cursus
	 * @see java.util.HashSet#clone()
	 */
	@Override
	public Object clone() {
		return cursussen.clone();
	}

	@Override
	public void createObject() throws CursussenException, CloneNotSupportedException, InschrijvingenException,
			LectorenException, Exception {
		try {
			Cursus cursus = null;
			if (inputFile.equalsIgnoreCase("src/persitentyfiles/Cursus.txt")) {
				cursus = new Cursus();
			}
			if (cursus != null) {
				cursus = cursus.maakObjectVanTekst(objectAlsTekst);
				cursus.addVoorgaandeOpleidingenVanTekst(objectAlsTekst, this);
				cursussen.add(cursus);
				cursusID = (cursusID < cursus.getID() ? cursus.getID() : cursusID);// Update hoogste
																					// huidige
																					// cursusid
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}
}
